#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<cstring>
#include<string>
#define FILEMOD 0666
using namespace std;
const char *FILENAME = "./fifo_file";

int main(){
    if(-1 == mkfifo(FILENAME,FILEMOD)){
        cout<<"mkfifo : "<<strerror(errno)<<endl;
        exit(errno);
    }

    int fd = open(FILENAME,O_WRONLY,FILEMOD);
    if(-1 == fd){
        cout<<"open : "<<strerror(errno)<<endl;
        exit(errno);
    }

    while(1){
        string s;getline(cin,s);
        if(s=="quit") break;
        if(write(fd,s.c_str(),s.size()) == -1){ //管道满就阻塞,读端关闭就被kill
            cout<<"write : "<<strerror(errno)<<endl;
            exit(errno);
        }
    }

    close(fd);
    cout<<unlink(FILENAME);//删除管道

    return 0;
}